﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Diagnostics;
using System.IO;
using System.Drawing;

namespace DemoExportPDF
{
    public class Report
    {
        public static void ExportPdf(int TestId)
        {
            // path save export file
            string pathExport = @"C:\Users\Tan\Desktop\Export";

            // create new document
            Document doc = new Document(PageSize.A4, 45F, 45F, 20F, 20F);

            // setup font for document
            iTextSharp.text.Font normal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

            // setup header for document
            iTextSharp.text.Font fheader = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
            iTextSharp.text.Font fheaderBold = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 11, iTextSharp.text.Font.BOLD, BaseColor.BLACK);

            // setup font for document
            iTextSharp.text.Font fColFlag = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, BaseColor.RED);

            #region header
            Paragraph paragraphHeader = new Paragraph();

            #region Title            

            // Title
            Phrase pTitle = new Phrase();

            // title bold for document
            Chunk c_Title = new Chunk("URINE DRUG SCREEN REPORT", fheader);       
            pTitle.Add(c_Title);

            #endregion title

            // add title to paragraph header
            paragraphHeader.Add(pTitle);

            // font for paragraph header
            paragraphHeader.Alignment = Element.ALIGN_CENTER;

            #endregion

            // create Directory
            if (!Directory.Exists(pathExport))
                Directory.CreateDirectory(pathExport);
            // create file name
            string filename =  pathExport + "/ref_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + ".pdf";
            // create pdf file
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));

            // wirte table info

            #region paragraph information test
            Paragraph pTestInformation = new Paragraph();

            pTestInformation.Add(new Phrase("Patient Name:   John Doe" + Environment.NewLine, normal));
            pTestInformation.Add(new Phrase("Patient ID:        88392" + Environment.NewLine, normal));
            pTestInformation.Add(new Phrase(Environment.NewLine, normal));
            pTestInformation.Add(new Phrase("Date of Test:    August 6, 2014" + Environment.NewLine, normal));
            pTestInformation.Add(new Phrase("Test Device:     Alere iCup Dx" + Environment.NewLine, normal));
            #endregion

            #region paragraph patient information
            Paragraph pPatientInformation = new Paragraph();
            pPatientInformation.Add(new Phrase("Brookline Pain Clinic" + Environment.NewLine, fheader));
            pPatientInformation.Add(new Phrase("57 Aspinwall Avenue" + Environment.NewLine, normal));
            pPatientInformation.Add(new Phrase("Brookline, MA  02446 " + Environment.NewLine, normal));
            #endregion


            PdfPTable table_Information = new PdfPTable(3);

            #region table information seting
            table_Information.WidthPercentage = 100;
            table_Information.SpacingBefore = 30;
            // setup width for table
            float[] widths = new float[] { 265f, 380f, 265f };
            table_Information.SetWidths(widths);
            #endregion

            PdfPCell cell1 = new PdfPCell();
            cell1.Border = PdfPCell.NO_BORDER;


            #region column test information for information table
            PdfPCell cell_col_TestInformation = new PdfPCell(pTestInformation);
            cell_col_TestInformation.Padding = 3;
            cell_col_TestInformation.Border = PdfPCell.NO_BORDER;
            cell_col_TestInformation.SetLeading(0, 1.3f);
            #endregion

            #region column patient information for information table
            PdfPCell cell_col_PatientInformation = new PdfPCell(pPatientInformation);
            cell_col_PatientInformation.Padding = 3;
            cell_col_PatientInformation.Border = PdfPCell.NO_BORDER;
            cell_col_PatientInformation.SetLeading(0, 1.3f);
            #endregion

            // add columns test information and patient information to information table
            cell1.AddElement(new Phrase());            
            table_Information.AddCell(cell_col_TestInformation);
            table_Information.AddCell(cell1);
            table_Information.AddCell(cell_col_PatientInformation);

            #region Column 1 - test
            Paragraph p_ColTest = new Paragraph();
            // header
            p_ColTest.Add(new Phrase("TEST" + Environment.NewLine, fheaderBold));
            // rows
            p_ColTest.Add(new Phrase(
                // type for TEST
                            "Amphetamine" + Environment.NewLine +
                            "Barbiturates" + Environment.NewLine +

                            "Benzodiazepines" + Environment.NewLine +
                            "Cocaine" + Environment.NewLine +

                            "MDMA" + Environment.NewLine +
                            "Methamphetamine" + Environment.NewLine +

                            "Methadone" + Environment.NewLine +
                            "Opiates" + Environment.NewLine +

                            "Oxycodone" + Environment.NewLine +
                            "Phencyclidine" + Environment.NewLine +

                            "Tricyclic Antidepressants" + Environment.NewLine +
                            "Cannabinoid" + Environment.NewLine +

                             Environment.NewLine +
                            // type for common TEST
                            "Creatinine" + Environment.NewLine +
                            "Nitrite" + Environment.NewLine +
                            "pH" + Environment.NewLine +
                            "Bleach" + Environment.NewLine +
                            "Specific Gravity" + Environment.NewLine, normal));
            p_ColTest.SpacingBefore = 18;
            #endregion

            #region column 2 - RESULT
            Paragraph p_ColResult = new Paragraph();
            // header
            p_ColResult.Add(new Phrase("RESULT" + Environment.NewLine, fheaderBold));
            // rows
            p_ColResult.Add(new Phrase(
                            // type for TEST
                            "Negative" + Environment.NewLine +
                            "Negative" + Environment.NewLine +

                            "Negative" + Environment.NewLine +
                            Environment.NewLine +

                            "Negative" + Environment.NewLine +
                            "Negative" + Environment.NewLine +

                            "Negative" + Environment.NewLine +
                            "Negative" + Environment.NewLine +

                            Environment.NewLine +
                            "Negative" + Environment.NewLine +

                            "Negative" + Environment.NewLine +
                            "Negative" + Environment.NewLine +

                            Environment.NewLine +

                            // type for common TEST
                            "Normal" + Environment.NewLine +
                            "Normal" + Environment.NewLine +
                            "Normal" + Environment.NewLine +
                            "Normal" + Environment.NewLine +
                            "Normal" + Environment.NewLine, normal));
            p_ColResult.SpacingBefore = 18;
            #endregion

            #region column 3 - Flag
            Paragraph p_ColFlag = new Paragraph();
            // header
            p_ColFlag.Add(new Phrase("FLAG" + Environment.NewLine, fheaderBold));
            // rows
            p_ColFlag.Add(new Phrase(
                            Environment.NewLine +
                            Environment.NewLine +

                            Environment.NewLine +
                            "Positive" + Environment.NewLine +

                            Environment.NewLine +
                            Environment.NewLine +

                            Environment.NewLine +
                            Environment.NewLine +

                            "Unclear" + Environment.NewLine +
                            Environment.NewLine +

                            Environment.NewLine +
                            Environment.NewLine, fColFlag));
            p_ColFlag.SpacingBefore = 18;
            #endregion

            #region column 4 - Cut-Off (ng/ml)
            Paragraph p_ColCutOff = new Paragraph();
            // header
            p_ColCutOff.Add(new Phrase("Cut-Off (ng/ml)" + Environment.NewLine, fheaderBold));
            // rows
            p_ColCutOff.Add(new Phrase(
                            "1000" + Environment.NewLine +
                            "300" + Environment.NewLine +

                            "300" + Environment.NewLine +
                            "300" + Environment.NewLine +

                            "500" + Environment.NewLine +
                            "1000" + Environment.NewLine +

                            "300" + Environment.NewLine +
                            "2000" + Environment.NewLine +

                            "100" + Environment.NewLine +
                            "25" + Environment.NewLine +

                            "1000" + Environment.NewLine +
                            "50" + Environment.NewLine, normal));
            p_ColCutOff.SpacingBefore = 18;
            #endregion
         

            PdfPTable table_Results = new PdfPTable(4);
            float[] ws = new float[] { 160, 160, 110, 110 };
            table_Results.SetWidths(ws);
            table_Results.SpacingBefore = 30;
            table_Results.WidthPercentage = 100;

            table_Results.DefaultCell.SetLeading(0, 1.3f);

            PdfPCell cell_ColTest = new PdfPCell(p_ColTest);            
            cell_ColTest.SetLeading(0, 1.6f);
            cell_ColTest.Border = PdfPCell.NO_BORDER;
            table_Results.AddCell(cell_ColTest);

            cell_ColTest.BackgroundColor = (new iTextSharp.text.BaseColor(System.Drawing.Color.FromArgb(217, 217, 217)));//new iTextSharp.text.BaseColor(255, 33, 33);

            PdfPCell cell_ColResult = new PdfPCell(p_ColResult);
            cell_ColResult.Rowspan = 2;
            cell_ColResult.SetLeading(0, 1.6f);
            cell_ColResult.Border = PdfPCell.NO_BORDER;
            table_Results.AddCell(cell_ColResult);

            PdfPCell cell_ColFlag = new PdfPCell(p_ColFlag);
            cell_ColFlag.Rowspan = 2;
            cell_ColFlag.SetLeading(0, 1.6f);
            cell_ColFlag.Border = PdfPCell.NO_BORDER;
            table_Results.AddCell(cell_ColFlag);

            PdfPCell cell_ColCutOff = new PdfPCell(p_ColCutOff);
            cell_ColCutOff.Rowspan = 2;
            cell_ColCutOff.SetLeading(0, 1.6f);
            cell_ColCutOff.Border = PdfPCell.NO_BORDER;
            table_Results.AddCell(cell_ColCutOff);

            table_Results.DefaultCell.Border = PdfPCell.NO_BORDER;

            Paragraph p_Comment = new Paragraph();
            //p_Comment.Add(new Phrase("COMMENTS:", fheader));
            p_Comment.Add(new Phrase("Inconsistent results.  Confirm at laboratory threshold.", normal));
            PdfPCell cell_Comment = new PdfPCell(p_Comment);
            cell_Comment.MinimumHeight = 50f;

            PdfPTable table_Comment = new PdfPTable(1);
            float[] listWidthComment = new float[] { 160};
            table_Comment.SetWidths(listWidthComment);
            table_Comment.SpacingBefore = 30;
            table_Comment.WidthPercentage = 100;
            //table_Comment.
            table_Comment.AddCell(cell_Comment);
            table_Comment.DefaultCell.Border = PdfPCell.NO_BORDER;


            // document struct
            #region document struct
            doc.Open();
            doc.Add(paragraphHeader);
            doc.Add(table_Information);
            doc.Add(table_Results);
            //doc.Add(new Chunk(Environment.NewLine));
            //doc.Add(new Chunk(Environment.NewLine));
            doc.Add(new Chunk(Environment.NewLine));
            doc.Add( new Chunk("COMMENTS:", fheaderBold));
            doc.Add(table_Comment);
            doc.Close();
            #endregion

            Process.Start(filename);
        }
    }
}
